<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Order2 extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }

                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }

                if (isset($filter['sa_name'])) {
                    $where[] = ['sa.name', 'like', '%' . $filter['sa_name'] . '%'];
                }

                if (isset($filter['shop_name'])) {
                    $where[] = ['s.shop_name', 'like', '%' . $filter['shop_name'] . '%'];
                }

                if (isset($filter['phone'])) {
                    $where[] = ['o.phone', 'like', '%' . $filter['phone'] . '%'];
                }

                if (isset($filter['yc'])) {
                    $where[] = ['o.yc', '=',$filter['yc']];
                }

                if (isset($filter['kf_status_time'])) {
                    $create_time = explode(' - ', $filter['kf_status_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.kf_status_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }

                if (isset($filter['cw_status_time'])) {
                    $create_time = explode(' - ', $filter['cw_status_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.cw_status_time', 'between', [$create_time[0], $create_time[1]]];
                }

//                if (isset($filter['nickname'])) {
//                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
//                }
//                if (isset($filter['province'])) {
//                    $province = Db::name('area')->find($filter['province']);
//                    $where[] = ['h.province', '=', $province['name']];
//                }
//                if (isset($filter['city'])) {
//                    $city = Db::name('area')->find($filter['city']);
//                    $where[] = ['h.city', '=', $city['name']];
//                }


//                if (isset($filter['time'])) {
//                    $create_time = explode(' - ', $filter['time']);
//                    foreach ($create_time as $k => $v) {
//                        $create_time[$k] = strtotime($v);
//                    }
//                    $where[] = ['o.time', 'between', [$create_time[0], $create_time[1]]];
//                }

            }
            //获取列表
            $_list = Db::name('order')->alias('o')
                ->leftJoin('salesperson sa', 'o.salesperson_id=sa.id')
                ->leftJoin('shop s', 'o.shop_id=s.id')
                ->field('o.*,sa.name as sa_name,sa.id as sa_id,s.id as s_id,s.shop_name')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['kf_status_time']) {
                    $_list[$k]['kf_status_time'] = date('Y-m-d H:i:s', $v['kf_status_time']);
                } else {
                    $_list[$k]['kf_status_time'] = '--------';
                }
                if ($v['cw_status_time']) {
                    $_list[$k]['cw_status_time'] = date('Y-m-d H:i:s', $v['cw_status_time']);
                } else {
                    $_list[$k]['cw_status_time'] = '--------';
                }

            }
            //获取数量
            $total = Db::name('order')->alias('o')
                ->leftJoin('salesperson sa', 'o.salesperson_id=sa.id')
                ->leftJoin('shop s', 'o.shop_id=s.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign([
            'role'=>1
        ]);
        $this->assign('proviec', $proviec);
        return $this->fetch();
    }

    public function kfstatus()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$data['row']['kf_status']) {
                $this->error('选择审核状态');
            }
            //  ["id"] => string(2) "15"
            //  ["shop_id"] => string(1) "8"
            //  ["salesperson_id"] => string(1) "7"
            $update_data = array(
                'id' => $data['id'],
                'package_id' => $data['row']['package_id'],
                'sc_money' => $data['row']['sc_money'],

                'hm_img' => $data['row']['hm_img'],
                'rz_img' => $data['row']['rz_img'],
                'cns_img' => $data['row']['cns_img'],

                'kf_status' => $data['row']['kf_status'],
                'kf_status_time' => time(),
                'kf_refuse' => $data['row']['kf_refuse'],
            );
            //套餐佣金
            $pack_info = Db::name('package')->find($data['row']['package_id']);
            //商家特殊返现
            $package_reward1 = Db::name('package_reward')->where([
                'is_del' => 0,
                'type' => 1,
                'package_id' => $data['row']['package_id'],
                'shop_id' => $data['shop_id'],
            ])->find();
            //员工特殊返现
            $package_reward2 = Db::name('package_reward')->where([
                'is_del' => 0,
                'type' => 2,
                'package_id' => $data['row']['package_id'],
                'salesperson_id' => $data['salesperson_id'],
            ])->find();

            if ($data['row']['reward1']) {
                $update_data['reward1'] = $data['row']['reward1'];
                setPackageReward(1, $data['row']['package_id'], $data['shop_id'], $data['row']['reward1']);
            } else {
                if ($package_reward1) {
                    $update_data['reward1'] = $package_reward1['reward1'];
                } else {
                    $update_data['reward1'] = $pack_info['reward1'];
                }
            }
            if ($data['row']['commission1']) {
                $update_data['commission1'] = $data['row']['commission1'];
                setPackageReward(1, $data['row']['package_id'], $data['shop_id'], 0, $data['row']['commission1']);
            } else {
                if ($package_reward1) {
                    $update_data['commission1'] = $package_reward1['commission1'];
                } else {
                    $update_data['commission1'] = $pack_info['commission1'];
                }
            }

            if ($data['row']['reward2']) {
                $update_data['reward2'] = $data['row']['reward2'];
                setPackageReward(2, $data['row']['package_id'], $data['salesperson_id'], $data['row']['reward2']);
            } else {
                if ($package_reward2) {
                    $update_data['reward2'] = $package_reward2['reward2'];
                } else {
                    $update_data['reward2'] = $pack_info['reward2'];
                }
            }

            if ($data['row']['commission2']) {
                $update_data['commission2'] = $data['row']['commission2'];
                setPackageReward(2, $data['row']['package_id'], $data['salesperson_id'], 0, $data['row']['commission2']);
            } else {
                if ($package_reward2) {
                    $update_data['commission2'] = $package_reward2['commission2'];
                } else {
                    $update_data['commission2'] = $pack_info['commission2'];
                }
            }

            $update_data['top_commission2'] = $pack_info['top_commission2'];
            if ($data['row']['kf_status'] == 2) {
                $update_data['status'] = 2;
            }
            if ($data['row']['kf_status'] == 1) {
                $update_data['status'] = 0;
            }
            Db::name('order')->update($update_data);
            $this->success('操作成功！');
        }
        $order_info = Db::name('order')->find($id);
        //获取门店信息
        $shop_info=Db::name('shop')->find($order_info['shop_id']);
        //获取套餐列表
        $pack_list = Db::name('package')->where([
            'status' => 1,
            'is_del' => 0,
            'province' => $shop_info['province'],
        ])->select();
        $this->assign([
            'data' => $order_info,
            'packList' => $pack_list
        ]);
        return $this->fetch();
    }

    public function cwstatus()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $order_info=Db::name('order')->find($data['id']);
            if ($order_info['kf_status']!=1){
                $this->error('客服审核未通过，请先客服审核');
            }
            if (!$data['row']['cw_status']) {
                $this->error('选择审核状态');
            }
            $update_data = array(
                'id' => $data['id'],
                'cw_status' => $data['row']['cw_status'],
                'status' => $data['row']['cw_status'],
                'cw_refuse' => $data['row']['cw_refuse'],
                'cw_status_time' => time(),
            );
            Db::name('order')->update($update_data);

            if ($data['row']['cw_status'] == 1) {
                $order_info = Db::name('order')->find($id);
                orderSendMoney($order_info);
            }
            $this->success('操作成功！');
        }
        $order_info = Db::name('order')->find($id);
        //获取门店信息
        $shop_info=Db::name('shop')->find($order_info['shop_id']);
        //获取套餐列表
        $pack_list = Db::name('package')->where([
            'status' => 1,
            'is_del' => 0,
            'province' => $shop_info['province'],
        ])->select();
        $this->assign([
            'data' => $order_info,
            'packList' => $pack_list
        ]);
        return $this->fetch();
    }

    public function ycedit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update_data = array(
                'id' => $data['id'],
                'yc' => $data['row']['yc'],
                'money1' => $data['row']['money1'],
                'money2' => $data['row']['money2'],
                'is_z' => $data['row']['is_z'],
                'yc_desc' => $data['row']['yc_desc'],
                'is_yc'=>1,
                'yc_time' => time(),
            );
            Db::name('order')->update($update_data);
            if ($data['row']['money1']){
                //商家扣款
                $shop_info=Db::name('shop')->find($data['shop_id']);
                $member_info=Db::name('member')->find($shop_info['member_id']);
                setMoney($member_info['id'],0,$shop_info['id'],$data['row']['money1'],2,$data['id'],0,0,6);
            }
            if ($data['row']['money2']){
                $salesperson_info=Db::name('salesperson')->find($data['salesperson_id']);
                $member_info=Db::name('member')->find($salesperson_info['member_id']);
                //业务员扣款
                setMoney($member_info['id'],$salesperson_info['id'],0,$data['row']['money2'],2,$data['id'],0,0,5);
            }
            $this->success('操作成功！');
        }
        $order_info = Db::name('order')->find($id);
        $yc_list = array(
            ['id' => 1,
                'name' => '管理单停'
            ],
            ['id' => 2,
                'name' => '管理停机'
            ],
            ['id' => 3,
                'name' => '欠费单停'
            ],
            ['id' => 4,
                'name' => '欠费停机'
            ],
            ['id' => 5,
                'name' => '销户'
            ],
            ['id' => 6,
                'name' => '涉诈'
            ],
            ['id' => 7,
                'name' => '其他'
            ]
        );
        $this->assign([
            'data' => $order_info,
            'yc_list' => $yc_list,
        ]);
        return $this->fetch();
    }

    public function ycedit2()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update_data = array(
                'id' => $data['id'],
                'yc' => $data['row']['yc'],
                'money1' => $data['row']['money1'],
                'money2' => $data['row']['money2'],
                'is_z' => $data['row']['is_z'],
                'yc_desc' => $data['row']['yc_desc'],
                'is_yc'=>1,
                'yc_time' => time(),
            );
            Db::name('order')->update($update_data);
            if ($data['row']['money1']){
                //商家扣款
                $shop_info=Db::name('shop')->find($data['shop_id']);
                $member_info=Db::name('member')->find($shop_info['member_id']);
                setMoney($member_info['id'],0,$shop_info['id'],$data['row']['money1'],2,$data['id'],0,0,6);
            }
            if ($data['row']['money2']){
                $salesperson_info=Db::name('salesperson')->find($data['salesperson_id']);
                $member_info=Db::name('member')->find($salesperson_info['member_id']);
                //业务员扣款
                setMoney($member_info['id'],$salesperson_info['id'],0,$data['row']['money2'],2,$data['id'],0,0,5);
            }
            $this->success('操作成功！');
        }
        $order_info = Db::name('order')->find($id);
        $yc_list = array(
            ['id' => 1,
                'name' => '管理单停'
            ],
            ['id' => 2,
                'name' => '管理停机'
            ],
            ['id' => 3,
                'name' => '欠费单停'
            ],
            ['id' => 4,
                'name' => '欠费停机'
            ],
            ['id' => 5,
                'name' => '销户'
            ],
            ['id' => 6,
                'name' => '涉诈'
            ],
            ['id' => 7,
                'name' => '其他'
            ]
        );
        $this->assign([
            'data' => $order_info,
            'yc_list' => $yc_list,
        ]);
        return $this->fetch();
    }

    public function getc_package()
    {
        $param = $this->request->param();
        $query=getPackageMoney($param['id'],$param['order_id']);
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }
}